<!-- #INCLUDE VIRTUAL="/fepace/Common.asp"-->
<!-- #INCLUDE VIRTUAL="/fepace/Cache.asp" -->
<!-- #INCLUDE VIRTUAL="/fepace/Connection.asp" -->
<%
On Error Resume Next
' Connections

Dim lrstAux
Dim lstrSQL

Dim pIdEtapa
Dim pIdComp
Dim pNomeEtapa
Dim pDescricaoEtapa
Dim pRegrasEtapa
Dim pCategoria1
Dim pCategoria2
Dim pCategoria3
Dim pCategoria4
Dim pContexto

Set lrstAux = Server.CreateObject("ADODB.RecordSet")

pIdEtapa = Request.Form("hidIdEtapa") 
pIdComp = Request.Form("Competicao")
pNomeEtapa = Request.Form("NomeEtapa")
pDescricaoEtapa = Request.Form("DescricaoEtapa")
pRegrasEtapa = Request.Form("RegrasEtapa")
pCategoria1 = Request.Form("Categoria1")
pCategoria2 = Request.Form("Categoria2")
pCategoria3 = Request.Form("Categoria3")
pCategoria4 = Request.Form("Categoria4")
pContexto = Request.Form("hidContexto")

If pContexto = "Inserir" Then
	DBConnection1.BeginTrans
	lstrSQL = "Insert into Etapas (IdComp, NomeEtapa, DescricaoEtapa, RegrasEtapa) " & _ 
	          "                    Values( " & _
						pIdComp & ", '" & _
						pNomeEtapa & "', '" & _
						pDescricaoEtapa & "', '" & _
						pRegrasEtapa & "')" 
	'response.Write(lstrSQL)
	'response.End()
	call DBConnection1.Execute(lstrSQL)			
	lrstAux.Open "Select * From Etapas Where NomeEtapa = '" & pNomeEtapa & "'", DBConnection1, 1, 1
	pIdEtapa = lrstAux.Fields("IdEtapa")
	Call inserirEtapaCategoria()
	DBConnection1.CommitTrans
	lrstAux.Close
	Set lrstAux = Nothing
	
ElseIf pContexto = "Atualizar" Then

	DBConnection1.BeginTrans
	lstrSQL = "Update Etapas set IdComp = " & pIdComp & ", " & _
						"                  NomeEtapa = '" & pNomeEtapa & "', " & _
						"                  DescricaoEtapa = '" & pDescricaoEtapa & "', " & _
						"                  RegrasEtapa = '" & pRegrasEtapa & "'  "  & _ 
						"    Where IdEtapa = " & pIdEtapa
	call DBConnection1.Execute(lstrSQL)			
	
	Call excluirEtapaCategoria()
	Call inserirEtapaCategoria()
	
	DBConnection1.CommitTrans
	
ElseIf pContexto = "Excluir" Then

	DBConnection1.BeginTrans
	
	Call excluirEtapa()
	
	DBConnection1.CommitTrans

End If

if Not Err.number = 0 Then DBConnection1.RollbackTrans
DBConnection1.Close
Set DBConnection1 = Nothing
If pContexto = "Excluir" Then
	Response.Redirect "Etapas_list.asp"
Else
	Response.Redirect "Etapas_maint.asp?IdEtapa=" & pIdEtapa
End If

Sub inserirEtapaCategoria()
	If Not pCategoria1 = 0 Then
		lstrSQL = "Insert into EtapaCategoria (IdEtapa, IdCategoria) " & _ 
							"                    Values( " & _
							pIdEtapa & ", " & _
							pCategoria1 & ")" 
		call DBConnection1.Execute(lstrSQL)			
	End If
	If Not pCategoria2 = 0 Then
		lstrSQL = "Insert into EtapaCategoria (IdEtapa, IdCategoria) " & _ 
							"                    Values( " & _
							pIdEtapa & ", " & _
							pCategoria2 & ")" 
		call DBConnection1.Execute(lstrSQL)			
	End If
	If Not pCategoria3 = 0 Then
		lstrSQL = "Insert into EtapaCategoria (IdEtapa, IdCategoria) " & _ 
							"                    Values( " & _
							pIdEtapa & ", " & _
							pCategoria3 & ")" 
		call DBConnection1.Execute(lstrSQL)			
	End If
	If Not pCategoria4 = 0 Then
		lstrSQL = "Insert into EtapaCategoria (IdEtapa, IdCategoria) " & _ 
							"                    Values( " & _
							pIdEtapa & ", " & _
							pCategoria4 & ")" 
		call DBConnection1.Execute(lstrSQL)			
	End If
End Sub

Sub excluirEtapa()

	lstrSQL = "Delete From Etapas Where IdEtapa = " & pIdEtapa
	call DBConnection1.Execute(lstrSQL)			

End Sub

Sub excluirEtapaCategoria()

	lstrSQL = "Delete From EtapaCategoria Where IdEtapa = " & pIdEtapa
	call DBConnection1.Execute(lstrSQL)			

End Sub

%>
